﻿#include "largefileupload.h"

LargeFileUpload::LargeFileUpload()
{
    id = 0;
    md5 = "";
    absolutePath = "";
    uploadedSize = 0;
    totalSize = 0;
    uploadStatus = 0;
}

LargeFileUpload::~LargeFileUpload()
{
}

uint64_t LargeFileUpload::getId() const
{
    return id;
}

void LargeFileUpload::setId(const uint64_t &value)
{
    id = value;
}

QString LargeFileUpload::getMd5() const
{
    return md5;
}

void LargeFileUpload::setMd5(const QString &value)
{
    md5 = value;
}

QString LargeFileUpload::getFileName() const
{
    return fileName;
}

void LargeFileUpload::setFileName(const QString &value)
{
    fileName = value;
}

QString LargeFileUpload::getAbsolutePath() const
{
    return absolutePath;
}

void LargeFileUpload::setAbsolutePath(const QString &value)
{
    absolutePath = value;
}

uint64_t LargeFileUpload::getUploadedSize() const
{
    return uploadedSize;
}

void LargeFileUpload::setUploadedSize(const uint64_t &value)
{
    uploadedSize = value;
}

uint64_t LargeFileUpload::getTotalSize() const
{
    return totalSize;
}

void LargeFileUpload::setTotalSize(const uint64_t &value)
{
    totalSize = value;
}

uint8_t LargeFileUpload::getUploadStatus() const
{
    return uploadStatus;
}

void LargeFileUpload::setUploadStatus(const uint8_t &value)
{
    uploadStatus = value;
}

QByteArray LargeFileUpload::toJson(LargeFileUpload &largeFileUpload)
{
    QJsonObject jsonObj;
    jsonObj.insert("id", QString::number(largeFileUpload.getId(), 10));
    jsonObj.insert("md5", largeFileUpload.getMd5());
    jsonObj.insert("fileName", largeFileUpload.getFileName());
    jsonObj.insert("absolutePath", largeFileUpload.getAbsolutePath());
    jsonObj.insert("uploadedSize", QString::number(largeFileUpload.getUploadedSize(), 10));
    jsonObj.insert("totalSize", QString::number(largeFileUpload.getTotalSize(), 10));
    jsonObj.insert("uploadStatus", QString::number(largeFileUpload.getUploadStatus()));
    return QJsonDocument(jsonObj).toJson();
}

LargeFileUpload LargeFileUpload::fromJson(QByteArray &byteArray)
{
    LargeFileUpload largeFileUpload;
    QJsonParseError jsonpe;
    QJsonDocument json = QJsonDocument::fromJson(byteArray, &jsonpe);
    if (jsonpe.error == QJsonParseError::NoError)
    {
        if (json.isObject())
        {
            QJsonObject obj = json.object();
            if (obj.contains("error"))
            {
                qWarning() << __FUNCTION__ << "error:" << obj["error"];
            }
            else
            {
                if (!obj.value("id").isNull() && !obj.value("id").isUndefined())
                    largeFileUpload.setId(obj.value("id").toString().toULongLong());

                if (!obj.value("fileName").isNull() && !obj.value("md5").isUndefined())
                    largeFileUpload.setFileName(obj.value("fileName").toString());

                if (!obj.value("md5").isNull() && !obj.value("md5").isUndefined())
                    largeFileUpload.setMd5(obj.value("md5").toString());

                if (!obj.value("absolutePath").isNull() && !obj.value("absolutePath").isUndefined())
                    largeFileUpload.setAbsolutePath(obj.value("absolutePath").toString());

                if (!obj.value("uploadedSize").isNull() && !obj.value("uploadedSize").isUndefined())
                    largeFileUpload.setUploadedSize(obj.value("uploadedSize").toString().toULongLong());

                if (!obj.value("totalSize").isNull() && !obj.value("totalSize").isUndefined())
                    largeFileUpload.setTotalSize(obj.value("totalSize").toString().toULongLong());

                if (!obj.value("uploadStatus").isNull() && !obj.value("uploadStatus").isUndefined())
                    largeFileUpload.setUploadStatus(obj.value("uploadStatus").toInt());
            }
        }
        else
        {
            qWarning() << __FUNCTION__ << "error, shoud json object";
        }
    }
    else
    {
        qWarning() << __FUNCTION__ << "error:" << jsonpe.errorString();
    }
    return largeFileUpload;
}

